//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "UI/Systems/LocaleManager.h"

namespace Engine
{
namespace UI
{
	
/*
=================================================
	constructor
=================================================
*/
	LocaleManager::LocaleManager (const EngineSubSystemsRef ess) :
		BaseObject(ess), _currentLocale(0)
	{
	}
		
/*
=================================================
	destructor
=================================================
*/
	LocaleManager::~LocaleManager ()
	{
	}
	
/*
=================================================
	Load
=================================================
*/
	bool LocaleManager::Load (uni_c_string filename)
	{
		Clear();

		CHECK_ERR( ESS()->GetResourceManager()->Load( EResource::UI_LOCALE_SETTINGS, filename, _settings ) );

		_currentLocale	= _settings->GetDefaultLocaleId();
		_filepath		= FileAddressUtils::GetPath( filename );

		return true;
	}
	
/*
=================================================
	Clear
=================================================
*/
	void LocaleManager::Clear ()
	{
		ESS()->GetResourceManager()->RemoveResources( EResource::UI_LOCALE_SETTINGS );
		ESS()->GetResourceManager()->RemoveResources( EResource::UI_LOCALE );

		_settings		= null;
		_currentLocale	= 0;
		_strings.Clear();
		_filepath.Clear();
	}
	
/*
=================================================
	_LoadLocaleResource
=================================================
*/
	bool LocaleManager::_LoadLocaleResource (uint fileID)
	{
		if ( fileID == 0 )
			return false;

		CHECK_ERR( _settings.IsNotNull() );

		UILocaleSettingsResource::files_map_t::iterator	res;

		CHECK_ERR( _settings->GetLocaleFiles().Find( fileID, res ) );

		UILocaleResourcePtr	locale;
		const usize			len = _filepath.Length();

		FileAddressUtils::AddNameToPath( _filepath, (uni_c_string) GetCurrentLanguageISO3() );
		_filepath << '_' << res->second << ".loc";

		CHECK_ERR( ESS()->GetResourceManager()->Load( EResource::UI_LOCALE, _filepath, locale ) );

		_filepath.SetLength( len );

		array<ext_string> const &	strings = locale->GetStrings();

		FOR( i, strings )
		{
			_strings.AddOrReplace( _BuildStringID( fileID, i+1 ), strings[i] );
		}

		return true;
	}
	
/*
=================================================
	IsLanguageSupported
=================================================
*/
	bool LocaleManager::IsLanguageSupported (uni_c_string iso3Name) const
	{
		CHECK_ERR( _settings.IsNotNull() );

		FOR( i, _settings->GetLanguages() )
		{
			if ( _settings->GetLanguages()[i].iso3Name == iso3Name )
				return true;
		}
		return false;
	}
	
/*
=================================================
	SetCurrentLanguage
=================================================
*/
	bool LocaleManager::SetCurrentLanguage (uni_c_string iso3Name)
	{
		CHECK_ERR( _settings.IsNotNull() );

		if ( _settings->GetLanguages()[_currentLocale].iso3Name == iso3Name )
			return true;

		FOR( i, _settings->GetLanguages() )
		{
			if ( _settings->GetLanguages()[i].iso3Name == iso3Name )
			{
				// Warning: to apply new language, you must reload all Widgets
				ESS()->GetGraphicsEngine()->GetFontLoader()->SetCharCache(
					_settings->GetLanguages()[i].charIntervals );

				_strings.Clear();
				_currentLocale = i;
				return true;
			}
		}
		return false;
	}


}	// UI
}	// Engine
